package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketStorage;
import com.cskaoyan.market.db.domain.MarketStorageExample;
import com.cskaoyan.market.db.mapper.MarketStorageMapper;
import com.cskaoyan.market.service.MarketStorageService;
import com.cskaoyan.market.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class MarketStorageServiceImpl implements MarketStorageService {
    @Override
    public void addStorage(MarketStorage marketStorage) {
        SqlSession session = MyBatisUtil.getSession();
        MarketStorageMapper mapper = session.getMapper(MarketStorageMapper.class);
        marketStorage.setDeleted(false);
        int insert = mapper.insert(marketStorage);
        session.commit();
        session.close();
    }
/**
 * ClassName: MarketStorageServiceImpl
 * Package: com.cskaoyan.market.service.impl
 * Date: 2023/5/8 - 16:23
 * Description:
 */
    @Override
    public List<MarketStorage> list(String page, String limit, String sort, String order, String userName, String key) {
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketStorageMapper marketStorageMapper = sqlSession.getMapper(MarketStorageMapper.class);
        MarketStorageExample marketStorageExample = new MarketStorageExample();
        //当根据key或name查找时，补充查找条件
        if (StringUtils.isEmpty(userName) && StringUtils.isEmpty(key)) {
            marketStorageExample.orderBy(sort + " " + order).or().andDeletedEqualTo(false);
        } else if (!(StringUtils.isEmpty(userName)) && StringUtils.isEmpty(key)) {
            marketStorageExample.orderBy(sort + " " + order).or().andDeletedEqualTo(false).andNameLike("%" + userName + "%");
        } else if (!(StringUtils.isEmpty(key)) && StringUtils.isEmpty(userName)) {
            marketStorageExample.orderBy(sort + " " + order).or().andDeletedEqualTo(false).andKeyEqualTo(key);
        } else {
            marketStorageExample.orderBy(sort + " " + order).or().andDeletedEqualTo(false).andKeyEqualTo(key).andNameLike("%" + userName + "%");
        }
        //分页准备
        Integer integerLimit = Integer.valueOf(limit);
        Integer integerPage = Integer.valueOf(page);
        //分页
        PageHelper.startPage(integerPage, integerLimit);
        //查找
        List<MarketStorage> marketStorages = marketStorageMapper.selectByExample(marketStorageExample);
        sqlSession.commit();
        sqlSession.close();
        //如果查出的数组为空，查找失败，返回null
        if (marketStorages == null) {
            return null;
        }
        //否则返回数组
        return marketStorages;
    }

    @Override
    public List<MarketStorage> updte(MarketStorage marketStorage) {
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketStorageMapper storageMapper = sqlSession.getMapper(MarketStorageMapper.class);
        MarketStorageExample marketStorageExample = new MarketStorageExample();
        marketStorageExample.or().andIdEqualTo(marketStorage.getId());
        int i = storageMapper.updateByExample(marketStorage, marketStorageExample);
        if (i != 1) {
            sqlSession.commit();
            sqlSession.close();
            return null;
        }
        List<MarketStorage> marketStorages = storageMapper.selectByExample(marketStorageExample);
        sqlSession.commit();
        sqlSession.close();
        return marketStorages;
    }

    @Override
    public void delet(MarketStorage marketStorage) {
        marketStorage.setDeleted(true);
        updte(marketStorage);
    }
}
